import { apiUrl } from "../../api.js";
import { StorageKeys } from "../../api.js";
import { getCategoryList } from "../../api.js";
import { getLocationList } from "../../api.js";
import { developing } from "../../api.js";

// var apiUrl = require('../../api.js');
const $api = require('../../utils/request.js');

const app = getApp();
Page({
  data: {
    message: '',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    categoryList: [],
    locationList: [],
    gridCol: 4,
    skin: false
  },

  onShow: function (e) {
    this.setData({
      categoryList: wx.getStorageSync(StorageKeys.CATEGORY_LIST).list,
      locationList: wx.getStorageSync(StorageKeys.LOCATION_LIST).list,
    })
  },

  inputBlur: function (e) {
    //获取 input 的值 (变量为 {{name}} )
    var name = e.currentTarget.dataset.name;
    var value = e.detail.value;
    //var data = '{"form":{"' + name + '":"' + value + '"}}';
    var data = '{"' + name + '":"' + value + '","message":""}';
    //值赋给 setData 供 save 接口的时候作为入参
    this.setData(JSON.parse(data));
  },

  saveCategory: function () {
    var obj = {
      categoryName: this.data.categoryName,
      memberId: wx.getStorageSync(StorageKeys.TOKEN)
      // locationCode: this.data.locationIndex,
      // locationName: this.data.picker[this.data.locationIndex]
    };

    // 调用接口获取openid
    $api.post(apiUrl + '/category/save', obj)
      .then(res => {
        console.log(res)
        //请求成功 
        this.setData({
          message: "保存成功",
          categoryList: this.data.categoryList.concat(obj)
        })
        getCategoryList();
        this.hideModal()
        console.log(res)
      })
      .catch(err => {
        this.setData({
          message: err
        })
        //请求失败
        console.log(err)
      })
  },

  saveLocation: function () {
    var obj = {
      locationName: this.data.locationName,
      memberId: wx.getStorageSync(StorageKeys.TOKEN)
      // locationCode: this.data.locationIndex,
      // locationName: this.data.picker[this.data.locationIndex]
    };
    console.log(obj)
    // 调用接口获取openid
    $api.post(apiUrl + '/location/save', obj)
      .then(res => {
        console.info("aaa", res)
        //请求成功 
        this.setData({
          message: "保存成功",
          locationList: this.data.locationList.concat(obj)
        })
        getLocationList();
        this.hideModal()
        console.log(res)
      })
      .catch(err => {
        this.setData({
          message: err
        })
        //请求失败
        console.log(err)
      })
  },

  developing(e) {
    developing()
  },

  showModal(e) {
    var modelName = e.currentTarget.dataset.target
    console.info('target', modelName)
    if (modelName === 'gridModal') {
      console.info('categoryList', this.data.categoryList.length)
      if(this.data.categoryList.length>=20){
        this.overlimit(20);
        return;
      }
    }
    else {
      console.info('locationList', this.data.locationList.length)
      if(this.data.locationList.length>=20){
        this.overlimit(20);
        return;
      }
    }
    this.setData({
      modalName: modelName,
      name: "",  //重新打开设置界面，值清空
      message: ""
    })
  },
  overlimit(e){
    wx.showModal({
      title: '提示',
      content: '已达上限值', //'这是一个模态弹窗',  
      showCancel: false,
      success(res) {
        wx.navigateBack({
          delta: 1, // 返回的页面数，默认 1（上一页）
          success() {
            console.log("返回成功");
          },
          fail(err) {
            console.error("返回失败", err);
          }
        });
      }
    }); 
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  gridchange: function (e) {
    this.setData({
      gridCol: e.detail.value
    });
  },
  gridswitch: function (e) {
    this.setData({
      gridBorder: e.detail.value
    });
  },
  menuBorder: function (e) {
    this.setData({
      menuBorder: e.detail.value
    });
  },
  menuArrow: function (e) {
    this.setData({
      menuArrow: e.detail.value
    });
  },
  menuCard: function (e) {
    this.setData({
      menuCard: e.detail.value
    });
  },
  switchSex: function (e) {
    this.setData({
      skin: e.detail.value
    });
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
})